package com.wd.tw.utils;

import com.wd.tw.domain.dto.response.EventData;
import com.wd.tw.domain.vo.TestData;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.Map;

/**
 * @className: Test
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2024/11/17
 **/
public class Test {

    private static String API = "https://192.168.5.213/api/json";

    //    public static void main(String[] args) {
//
//        UserLogin userLogin = new UserLogin();
//        userLogin.setUser_name("admin");
//        userLogin.setUser_pwd("admin@123");
//
//        String post = HttpUtils.post(API, JSONObject.toJSONString(userLogin));
//        System.out.println(post);
//    }
    // 关闭 SSL 证书验证的方法
    // 关闭 SSL 证书验证的方法
    public static void disableCertificateValidation() throws Exception {
        // 创建一个信任所有证书的 TrustManager
        TrustManager[] trustAllCertificates = new TrustManager[]{
                new X509TrustManager() {
                    // 不验证服务器端证书
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }

                    // 这两个方法为空实现，跳过验证
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    }

                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    }
                }
        };

        // 创建 SSLContext，使用不验证证书的 TrustManager
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCertificates, new java.security.SecureRandom());

        // 设置默认的 SSLSocketFactory
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // 设置默认的 HostnameVerifier，跳过主机名验证
        HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
    }

    // 使用不验证证书的 SSL 连接发送 POST 请求
    public static String sendPostRequest(String requestBody, Map<String, String> headerMap) throws Exception {
        // 目标 URL
        URL url = new URL(API);  // 替换为目标 POST 接口 URL
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 启用输入输出流
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");

        // 设置请求头为 application/json
        connection.setRequestProperty("Content-Type", "application/json");

        if (headerMap != null) {
            for (String key : headerMap.keySet()) {
                connection.setRequestProperty(key, headerMap.get(key));
            }
        }

        // 发送 POST 请求
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = requestBody.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        // 获取响应
        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);

        // 读取响应数据
        try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            String inputLine;
            StringBuilder content = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }

            // 打印响应内容
            System.out.println("Response Body: " + content.toString());
            return content.toString();
        } finally {
            connection.disconnect();
        }
    }

    public static void main(String[] args) {
        TestData testData = new TestData();
        System.out.println(testData.getTime());
        System.out.println(testData.getTm());
    }
}
